

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.SessionState;

using System.Web.Configuration;
using System.Web.Profile;
using JTC.MiniCommerce.Core;
using JTC.MiniCommerce.Store;

namespace JTC.MiniCommerce.Web {
  public class Global : System.Web.HttpApplication {

    /// <summary>
    /// Handles the Start event of the Application control.
    /// </summary>
    /// <param name="sender">The source of the event.</param>
    /// <param name="e">The <see cref="T:System.EventArgs"/> instance containing the event data.</param>
    protected void Application_Start(object sender, EventArgs e) {
    }

    /// <summary>
    /// Handles the End event of the Application control.
    /// </summary>
    /// <param name="sender">The source of the event.</param>
    /// <param name="e">The <see cref="T:System.EventArgs"/> instance containing the event data.</param>
    protected void Application_End(object sender, EventArgs e) {

    }

    /// <summary>
    /// Handles the PreRequestHandlerExecute event of the Application control.
    /// </summary>
    /// <param name="sender">The source of the event.</param>
    /// <param name="e">The <see cref="T:System.EventArgs"/> instance containing the event data.</param>
    protected void Application_PreRequestHandlerExecute(object sender, EventArgs e) {
      SiteSettings siteSettings = null;
      try {
        siteSettings = SiteSettings.Load();
      }
      catch(Exception ex) {
        //swallow it - probably no connection string during install
      }
        
      string cultureName = ""; //default this. TODO: Get this into installer?
      if(siteSettings != null) {
        cultureName = siteSettings.Language == null ? cultureName : siteSettings.Language;
      }
      System.Threading.Thread.CurrentThread.CurrentCulture = System.Globalization.CultureInfo.CreateSpecificCulture(cultureName);
      System.Threading.Thread.CurrentThread.CurrentUICulture = System.Threading.Thread.CurrentThread.CurrentCulture;
    }

    /// <summary>
    /// Handles the OnMigrateAnonymous event of the Profile control.
    /// </summary>
    /// <param name="sender">The source of the event.</param>
    /// <param name="args">The <see cref="T:System.Web.Profile.ProfileMigrateEventArgs"/> instance containing the event data.</param>
    public void Profile_OnMigrateAnonymous(object sender, ProfileMigrateEventArgs args) {
      OrderController.MigrateCart(args.AnonymousID, WebProfile.Current.UserName);
      ProfileManager.DeleteProfile(args.AnonymousID);
      AnonymousIdentificationModule.ClearAnonymousIdentifier();
    }
  }
}